package it.sd.pwframework.business.service.impl;

import it.sd.pwframework.business.service.SpiderService;
import it.sd.utility.XMLWrapper;
import it.sd.wdc.spider.WdcSpiderRequest;
import it.sd.wdc.spider.executer.ScriptExecuter;
import it.sd.wdc.spider.repository.impl.ScriptRepositoryDaoXML;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

@Service("spiderService")
public class SpiderServiceImpl implements SpiderService {

	private ScriptExecuter exe = null;

	
	@PostConstruct
	public void init() throws Exception {
		
		ScriptRepositoryDaoXML dao = new ScriptRepositoryDaoXML();
		dao.setFolderName("\\java\\workspace\\hibernate\\spider\\src\\main\\resources\\spiderscript");
		dao.setFileName("configuration-spider.xml");
		dao.load();
		
		ScriptExecuter exe = new ScriptExecuter();
		exe.setRepository(dao);
	
	}
	
	@Override
	public XMLWrapper executeService(String service, Map<String, String> params)
			throws Exception {
		
		WdcSpiderRequest ric = new WdcSpiderRequest();
		
		ric.setIdService(service);
		if (params!=null) {
			for (String key: params.keySet()) {
				ric.addParameter(key, params.get(key));
			}
		}
		
		exe.execute(ric);
		
		return ric.getOutput();

		
	}
	
	

}
